﻿#ifndef _OBSERVER_H
#define _OBSERVER_H

#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include "Subject.h"

class Observer
{
protected:
	Subject* _subject;
public:
	Observer(Subject* subject)
		:_subject(subject)
	{}

	virtual void Register() = 0;
	virtual void Remove() = 0;
	virtual void GetWeatherData() = 0;
	virtual void Display() = 0;
};

class ForecastDisplay :public Observer
{
private:
	float _temp;
	float _humidity;
	float _pressure;
	std::vector<std::string> _forecast;

public:
	ForecastDisplay(Subject* subject)
		:Observer(subject)
	{
		InitForecast();
		Register();
	}

	inline void InitForecast()
	{
			_forecast.push_back("⛅");
			_forecast.push_back("☔");
			_forecast.push_back("🌞");
			_forecast.push_back("☀️");
			_forecast.push_back("🌧️");
			_forecast.push_back("☁️");
			_forecast.push_back("🌩️");
			_forecast.push_back("🌪️");
			_forecast.push_back("☂️");
			_forecast.push_back("🌂");
			_forecast.push_back("❄️");
			_forecast.push_back("⛱️");
			_forecast.push_back("💧");
			_forecast.push_back("🌊");
			_forecast.push_back("☃️");
			_forecast.push_back("⚡");
			_forecast.push_back("🔥");
			_forecast.push_back("🌨️");
	}

	inline void GetWeatherData()
	{
		_subject->GetWeatherData(_temp, _humidity, _pressure);
	}

	inline void Register()
	{
		_subject->RegisterObserver(dynamic_cast<Observer*>(this));
	}

	inline void Remove()
	{
		_subject->RemoveObserver(dynamic_cast<Observer*>(this));
	}

	inline void Display()
	{
		GetWeatherData();
		std::cout << "############################天气预报############################" << std::endl;
		std::cout << "预计明天天气为: " << _forecast[rand() % _forecast.size()] << std::endl;
		std::cout << "############################当前状况############################" << std::endl;
		std::cout << "温度：" << _temp << "℃" << std::endl;
		std::cout << "湿度：" << _humidity << "%" << std::endl;
		std::cout << "气压：" << _pressure << "kPa" << std::endl;
	}
};

#endif
